package com.xposed.cdxposed.hook

import android.annotation.SuppressLint
import com.xposed.cdxposed.Constant
import com.xposed.cdxposed.XposedInit
import com.xposed.cdxposed.XposedInit.Companion.xsp
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge

/**
 * 描述：步数Hook
 *
 * @author jay on 2018/4/28 12:28
 */
object StepHook {
    @SuppressLint("PrivateApi")
    fun hook() {
        XposedBridge.log("cdcdcd开始hook步数啦")
        val c1 = Class.forName("android.hardware.SystemSensorManager\$SensorEventQueue")
        XposedBridge.log("cdcdcd获取到实class了SensorEventQueue")
        XposedBridge.hookAllMethods(c1, "dispatchSensorEvent", object : XC_MethodHook() {
            override fun beforeHookedMethod(param: MethodHookParam) {
                XposedBridge.log("cdcdcd调用dispatchSensorEvent方法啦---IS_STEP_OPEN="+XposedInit.xsp.getBoolean(Constant.IS_STEP_OPEN, false))
                xsp.reload()
                if (XposedInit.xsp.getBoolean(Constant.IS_STEP_OPEN, false)) {
                    XposedBridge.log("cdcdcd修改前的值是"+(param.args[1] as FloatArray)[0])
                    (param.args[1] as FloatArray)[0] = (param.args[1] as FloatArray)[0] * (xsp.getString(Constant.CUR_STEP_MULT, "1").toInt())
                    XposedBridge.log("cdcdcd修改后的值是"+(param.args[1] as FloatArray)[0])
                }
                super.beforeHookedMethod(param)
            }
        })
    }
}